 //控制层 
app.controller('specificationController' ,function($scope,$controller   ,specificationService){	
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		specificationService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		specificationService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		specificationService.findOne(id).success(response=>{
			$scope.entity = response;
		})
	}
	
	//保存 
	$scope.save=function(){				
		//判断是否有规格的id，再决定是添加还是修改操作？
		//1.得到规格的id值
		let id=$scope.entity.spec.id;
		//定义添加url
		let url="../specification/add.do";
		//2.判断id值
		if(id){
			url="../specification/update.do";
		}
		//3.调用服务层执行保存操作
		specificationService.save(url,$scope.entity).success(response=>{
			//弹窗提示
			if(response.success){
				alert(response.message)
			}else {
                alert(response.message);
			}
			//关闭模态框
			$("#editModal").modal("hide");
			//刷新页面
			$scope.findAll();
		})

	}
	
	 
	//批量删除 
	$scope.delSpec=function(){
		//获取选中的复选框			
		specificationService.delSpec( $scope.deleteIds ).success(
			function(response){
				if(response.success){
					$scope.findAll();//刷新列表
					$scope.deleteIds=[];
                    alert(response.message)
				}else {
                    alert(response.message);
				}
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		specificationService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	//为规格组合p对象初始化
	$scope.entity={spec:{},specificationOptionList:[]};
        //新增规格选项文本框
	$scope.addSpecOption=()=>{
        //向$scope.entity.specificationOptionList这个集合中添加一条空的规格选项框
		$scope.entity.specificationOptionList.push({});
	}
	//删除指定规格选项框
	$scope.deleteSpecOption=(index)=>{
		$scope.entity.specificationOptionList.splice(index,1);
	}
});	
